```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解WebFlux | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3 {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            color: #abb2bf;
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(167, 119, 227, 0.2);
            z-index: -1;
            transform: scaleX(1.02);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .diagram-container {
            background-color: #f8fafc;
            border-radius: 12px;
            padding: 2rem;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">深入理解 Spring WebFlux</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索响应式编程的革命性力量，构建高性能异步非阻塞应用</p>
            <div class="flex justify-center space-x-4">
                <a href="#what-is-webflux" class="bg-white text-indigo-700 px-6 py-3 rounded-full font-medium hover:bg-indigo-50 transition duration-300">
                    <i class="fas fa-book mr-2"></i>开始阅读
                </a>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
        <!-- What is WebFlux Section -->
        <section id="what-is-webflux" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-code text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">什么是 WebFlux</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8 card-hover">
                <div class="p-6">
                    <p class="text-gray-700 mb-4">
                        Spring Framework 中包含的原始 Web 框架 Spring Web MVC 是专门为 Servlet API 和 Servlet 容器构建的。<span class="font-bold text-indigo-600">反应式堆栈</span> Web 框架 Spring WebFlux 是在 5.0 版的后期添加的。它是完全非阻塞的，支持反应式流(Reactive Stream)背压，并在Netty，Undertow和Servlet 3.1 +容器等服务器上运行。
                    </p>
                    
                    <div class="my-8">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651836760441-b2e78446-8913-4842-a6c7-b93931e4bc5a.png" 
                             alt="WebFlux架构图" class="rounded-lg w-full shadow-md">
                        <p class="text-sm text-gray-500 mt-2 text-center">WebFlux架构示意图</p>
                    </div>
                    
                    <div class="my-8">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651836938603-c4656c7a-0f34-43f7-85fa-f4f3a4162ab0.png" 
                             alt="WebFlux与传统MVC对比" class="rounded-lg w-full shadow-md">
                        <p class="text-sm text-gray-500 mt-2 text-center">WebFlux与传统MVC对比</p>
                    </div>
                    
                    <p class="text-gray-700 mb-6">
                        Spring WebFlux 是一个<span class="font-bold text-indigo-600">异步非阻塞式 IO</span> 模型，通过少量的容器线程就可以支撑大量的并发访问，所以 Spring WebFlux 可以有效提升系统的吞吐量和伸缩性，特别是在一些 IO 密集型应用中，Spring WebFlux 的优势明显。例如微服务网关 Spring Cloud Gateway 就使用了 WebFlux，<span class="font-bold text-indigo-600">这样可以有效提升网关对下游服务的吞吐量</span>，底层使用的是 Netty 容器，这点也和传统的 SpringMVC 不一样，SpringMVC 是基于 Servlet 的。
                    </p>
                    
                    <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-6">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-exclamation-circle text-yellow-400"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-sm text-yellow-700">
                                    <strong>需要注意：</strong> 接口的响应时间并不会因为使用了 WebFlux 而缩短，服务端的处理结果还是得由 worker 线程处理完成之后再返回给前端。
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Async Blocking vs Non-blocking Section -->
        <section id="async-comparison" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-exchange-alt text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">异步阻塞和异步非阻塞区别</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                            <i class="fas fa-hourglass-half text-red-400 mr-2"></i>阻塞模式
                        </h3>
                        <p class="text-gray-700">
                            进程给CPU传达一个任务之后，一直等待CPU处理完成，然后才执行后面的操作。
                        </p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                            <i class="fas fa-sync-alt text-green-400 mr-2"></i>非阻塞模式
                        </h3>
                        <p class="text-gray-700">
                            进程给CPU传达任务后，继续处理后续的操作，隔断时间再来询问之前的操作是否完成。这样的过程其实也叫轮询。
                        </p>
                    </div>
                </div>
            </div>
            
            <div class="mb-8">
                <div class="code-block p-6 rounded-lg overflow-x-auto">
                    <pre><code class="language-java">
@RestController
public class TestController {

    @GetMapping("/test1")
    public String hello(String msg) {
        long start = System.currentTimeMillis();
        String helloStr = getHelloStr(msg);
        System.out.println("同步阻塞接口耗时：" + (System.currentTimeMillis() - start));
        return helloStr;
    }

    @GetMapping("/test2")
    public Mono&lt;String&gt; hello2(String msg) {
        long start = System.currentTimeMillis();
        Mono&lt;String&gt; hello2 = Mono.fromSupplier(() -> getHelloStr(msg));
        System.out.println("异步非阻塞(webflux)接口耗时：" + (System.currentTimeMillis() - start));
        return hello2;
    }

    private String getHelloStr(String msg) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello ---> "+msg;
    }
}
                    </code></pre>
                </div>
            </div>
            
            <div class="my-8">
                <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651837631961-983dc9dd-a713-4006-b1db-490d2d15b6ae.png" 
                     alt="性能对比图" class="rounded-lg w-full shadow-md">
                <p class="text-sm text-gray-500 mt-2 text-center">阻塞与非阻塞性能对比</p>
            </div>
            
            <div class="bg-indigo-50 rounded-xl p-6 mb-8">
                <p class="text-indigo-700">
                    通过上面不难发现非阻塞性能要比阻塞性能高很多，所以在一些 IO 密集型应用中，Spring WebFlux 的优势明显，这也是网关使用webflux的原因。
                </p>
            </div>
            
            <div class="diagram-container my-8">
                <div class="mermaid">
                    graph LR
                    A[客户端请求] --> B{WebFlux 处理}
                    B -->|非阻塞| C[少量线程处理多个请求]
                    B -->|传统阻塞| D[每个请求占用一个线程]
                    C --> E[高吞吐量]
                    D --> F[低吞吐量]
                </div>
            </div>
        </section>
        
        <!-- WebFlux API Section -->
        <section id="webflux-api">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-cogs text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">WebFlux定义接口</h2>
            </div>
            
            <div class="mb-8">
                <div class="code-block p-6 rounded-lg overflow-x-auto">
                    <pre><code class="language-java">
@Configuration
public class HelloController {

    @Bean
    public RouterFunction getHelloFunction() {

        // 1.设置请求方式和请求路径
        RequestPredicate path = RequestPredicates.GET("/hello"); // @GETMaping

        // 2.把path和接口响应内容绑定
        RouterFunction&lt;ServerResponse&gt; routerFunction = RouterFunctions.route(path, (request) -> {
            ServerHttpRequest req = request.exchange().getRequest();
            // 设置响应的数据
            Mono&lt;String&gt; data = Mono.just("Hello-" + System.currentTimeMillis());
            // 把数据写回到responce中
            return ServerResponse.ok().body(data, String.class);
        });

        // 3、追加接口的定义
        RouterFunction&lt;ServerResponse&gt; routerFunction1 = routerFunction.andRoute(RequestPredicates.GET("/demo"), request -> {
            return ServerResponse.ok().body(Mono.just("demo-" + System.currentTimeMillis()), String.class);
        });
        return routerFunction1;
    }
}
                    </code></pre>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8 card-hover">
                <div class="p-6">
                    <h3 class="text-xl font-bold text-gray-800 mb-4">WebFlux API解释</h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-server text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">ServerResponse</h4>
                                    <p class="text-gray-600 text-sm">类似于HttpServletResponse</p>
                                </div>
                            </div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-server text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">ServerRequest</h4>
                                    <p class="text-gray-600 text-sm">类似于HttpServletRequest</p>
                                </div>
                            </div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-project-diagram text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">RouteFunctions</h4>
                                    <p class="text-gray-600 text-sm">将路由和处理方法绑定</p>
                                </div>
                            </div>
                        </div>
                        <div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-code text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">Mono和Flux</h4>
                                    <p class="text-gray-600 text-sm">反应式编程核心类</p>
                                </div>
                            </div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-cogs text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">HandlerFunction</h4>
                                    <p class="text-gray-600 text-sm">处理方法(Controller中的业务逻辑操作)</p>
                                </div>
                            </div>
                            <div class="flex items-start mb-4">
                                <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-filter text-indigo-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-800">RequestPredicates</h4>
                                    <p class="text-gray-600 text-sm">定义请求方法,对请求方法的一些限制</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651838031088-c7d3689f-fa5f-486c-a2b2-e45f7e0d59a8.png" 
                         alt="WebFlux架构图" class="rounded-lg w-full shadow-md">
                </div>
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651838042531-fa503499-ea11-4f70-92d5-3d160bba29e0.png" 
                         alt="WebFlux流程图" class="rounded-lg w-full shadow-md">
                </div>
            </div>
        </section>
        
        <!-- Key Takeaways -->
        <section class="bg-indigo-50 rounded-xl p-8 my-16">
            <h3 class="text-2xl font-bold text-indigo-800 mb-6 flex items-center">
                <i class="fas fa-lightbulb text-indigo-500 mr-3"></i>关键要点总结
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="flex items-start">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                    </div>
                    <div class="ml-4">
                        <p class="text-gray-700">WebFlux 是完全非阻塞的，支持反应式流背压</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                    </div>
                    <div class="ml-4">
                        <p class="text-gray-700">在IO密集型应用中，WebFlux能显著提升吞吐量</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                    </div>
                    <div class="ml-4">
                        <p class="text-gray-700">微服务网关Spring Cloud Gateway基于WebFlux</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <div class="flex-shrink-0 mt-1">
                        <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                    </div>
                    <div class="ml-4">
                        <p class="text-gray-700">WebFlux不会减少单个请求的响应时间</p>
                    </div>
                </div>
            </div>
        </section>
    </div>
    
    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col items-center">
                <div class="text-2xl font-bold mb-4">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition duration-300">
                    <i class="fas fa-link mr-2"></i>http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>
    
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```